﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
using MySql.Data.MySqlClient;
using System.Data;


    namespace OpenSISDataConnection
    {
        /// <summary>
        /// Summary description for DataBaseConnectionDetails
        /// </summary>
        /// 


        public class DataConnection
        {
            public static class ConnectionPath
            {

                //public static string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["sqlConn"];
                //public static string server = System.Configuration.ConfigurationSettings.AppSettings["appserver"];

                public static string CnnStr = System.Web.Configuration.WebConfigurationManager.AppSettings["sqlConn"];
                public static string server = System.Web.Configuration.WebConfigurationManager.AppSettings["appserver"];

               // public static string CnnStr = "Persist Security Info=False;Provider=SQLOLEDB;Persist Security Info=False;User ID=osisnew2009;pwd=osisnew;Data Source=acdir;Database=osis_new;";
               // public static string server = "MSSQLServer";


                public static DataSet Getdata(string sql)
                {
                    if (server == "MSSQLServer")
                    {

                        OleDbConnection cnn = new OleDbConnection(CnnStr);
                        if (cnn.State == ConnectionState.Closed)
                        {
                            cnn.Open();
                        }

                        System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, cnn);
                        DataSet datads = new DataSet();
                        da.Fill(datads);
                        da.Dispose();

                        if (cnn.State == ConnectionState.Open)
                        {
                            cnn.Close();
                        }
                        return datads;
                    }
                    else
                    {
                        MySqlConnection cnn = new MySqlConnection(CnnStr);
                        if (cnn.State == ConnectionState.Closed)
                        {
                            cnn.Open();
                        }

                        MySqlDataAdapter da = new MySqlDataAdapter(sql, cnn);
                        DataSet datads = new DataSet();
                        da.Fill(datads);
                        da.Dispose();

                        if (cnn.State == ConnectionState.Open)
                        {
                            cnn.Close();
                        }


                        return datads;

                    }

                }

                public static bool ExecuteCommand(string strSQL)
                {

                    //string strConString = _ConnString;
                    //System.Data.OleDb.OleDbConnection conSQL = new System.Data.OleDb.OleDbConnection(strConString);
                    if (server == "MSSQLServer")
                    {
                        OleDbConnection cnn = new OleDbConnection(CnnStr);
                        //OleDbTransaction dd = new OleDbTransaction();
                        
                        System.Data.OleDb.OleDbCommand cmdSQL = new System.Data.OleDb.OleDbCommand();
                        cnn.Open();
                        cmdSQL.Connection = cnn;
                        cmdSQL.CommandText = strSQL;
                        cmdSQL.ExecuteNonQuery();
                        cnn.Close();
                        cnn.Dispose();
                        return true;
                    }
                    else
                    {
                        MySqlConnection cnn = new MySqlConnection(CnnStr);

                        MySqlCommand cmdSQL = new MySqlCommand();
                        cnn.Open();
                        cmdSQL.Connection = cnn;
                        cmdSQL.CommandText = strSQL;
                        cmdSQL.ExecuteNonQuery();
                        cnn.Close();
                        cnn.Dispose();
                        return true;

                    }
                }

                public static bool ExecuteCommandafterinstall(string strSQL, string cnnstring, string servernam)
                {
                    if (servernam == "MSSQLServer")
                    {
                        OleDbConnection cnn = new OleDbConnection(cnnstring);

                        System.Data.OleDb.OleDbCommand cmdSQL = new System.Data.OleDb.OleDbCommand();
                        cnn.Open();
                        cmdSQL.Connection = cnn;
                        cmdSQL.CommandText = strSQL;
                        cmdSQL.ExecuteNonQuery();
                        cnn.Close();
                        cnn.Dispose();
                        return true;

                    }
                    else
                    {
                        MySqlConnection cnn = new MySqlConnection(cnnstring);

                        MySqlCommand cmdSQL = new MySqlCommand();
                        cnn.Open();
                        cmdSQL.Connection = cnn;
                        cmdSQL.CommandText = strSQL;
                        cmdSQL.ExecuteNonQuery();
                        cnn.Close();
                        cnn.Dispose();
                        return true;

                    }



                }



                public static DataSet Getdataafterinstall(string sql, string Cnnstring, string servernam)
                {
                    if (servernam == "MSSQLServer")
                    {
                        OleDbConnection cnn = new OleDbConnection(Cnnstring);
                        if (cnn.State == ConnectionState.Closed)
                        {
                            cnn.Open();
                        }

                        System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sql, cnn);
                        DataSet datads = new DataSet();
                        da.Fill(datads);
                        da.Dispose();

                        if (cnn.State == ConnectionState.Open)
                        {
                            cnn.Close();
                        }


                        return datads;
                    }
                    else
                    {
                        MySqlConnection cnn = new MySqlConnection(Cnnstring);
                        if (cnn.State == ConnectionState.Closed)
                        {
                            cnn.Open();
                        }

                        MySqlDataAdapter da = new MySqlDataAdapter(sql, cnn);
                        DataSet datads = new DataSet();
                        da.Fill(datads);
                        da.Dispose();

                        if (cnn.State == ConnectionState.Open)
                        {
                            cnn.Close();
                        }


                        return datads;

                    }


                }


                //*********************************Mysql Configuration below*************************//



                public static DataSet CheckDatabase(string sql, string Cnnstring)
                {
                    MySqlConnection cnn = new MySqlConnection(Cnnstring);
                    if (cnn.State == ConnectionState.Closed)
                    {
                        cnn.Open();
                    }

                    MySqlDataAdapter da = new MySqlDataAdapter(sql, cnn);
                    DataSet datads = new DataSet();
                    da.Fill(datads);
                    da.Dispose();

                    if (cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }


                    return datads;
                }




            }
        }
    }
